package com.zx.kitchen.index.common.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.entity.DishesListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/5.
 */

public class DishListAdapter extends BaseAdapter {

    private List<DishesListBean> objects = new ArrayList<DishesListBean>();

    private Context context;
    private LayoutInflater layoutInflater;

    public DishListAdapter(Context context, List<DishesListBean> objects) {
        this.context = context;
        this.objects = objects;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public DishesListBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.adapter_dishlist, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((DishesListBean) getItem(position), (ViewHolder) convertView.getTag(), convertView, position);
        return convertView;
    }

    private void initializeViews(DishesListBean object, ViewHolder holder, View convertView, int position) {
        holder.dishName.setText(object.getName());
        holder.dishPrice.setText(object.getPrice() + "/" + object.getUnit());
        holder.dishStandard.setText(object.getBrand() + "  " + object.getStandard());
        final DishesListBean item = getItem(position);
        if (item.isChecked()) {
            holder.ivSelectedStatus.setImageResource(R.mipmap.icon_selected);
        } else {
            holder.ivSelectedStatus.setImageResource(R.mipmap.icon_unselected);

        }
        final ViewHolder finalViewHolder = holder;
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.isChecked()) {
                    finalViewHolder.ivSelectedStatus.setImageResource(R.mipmap.icon_unselected);
                } else {
                    finalViewHolder.ivSelectedStatus.setImageResource(R.mipmap.icon_selected);
                }
                item.setIsChecked(!item.isChecked());
                if (onCheckStatusChangeListener != null) {
                    onCheckStatusChangeListener.onChange(item.isChecked());
                }
            }
        });

    }

    private OnCheckStatusChangeListener onCheckStatusChangeListener;

    public interface OnCheckStatusChangeListener {
        void onChange(boolean isCheck);
    }

    public void setOnCheckStatusChangeListener(OnCheckStatusChangeListener onCheckStatusChangeListener) {
        this.onCheckStatusChangeListener = onCheckStatusChangeListener;
    }


    protected class ViewHolder {
        private TextView dishName;
        private TextView dishPrice;
        private TextView dishStandard;
        private ImageView ivSelectedStatus;

        public ViewHolder(View view) {
            dishName = (TextView) view.findViewById(R.id.dish_name);
            dishPrice = (TextView) view.findViewById(R.id.dish_price);
            dishStandard = (TextView) view.findViewById(R.id.dish_standard);
            ivSelectedStatus = (ImageView) view.findViewById(R.id.iv_selected_status);
        }
    }

}
